package com.qdxwx.manage.controller.card;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.card.CardCodeService;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.card.CardCode;
import com.qdxwx.models.enums.CardStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 课程卡主Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/card")
public class CardController extends BaseController {

    @Autowired
    private CardCodeService cardCodeService;

    /**
     * 通过主卡作废详卡表信息
     */
    @PostMapping("/closeAll.json")
    @ResponseBody
    public AjaxResult closeBycardId(HttpServletRequest request) {
        long cardId = TKConvert.toLong(request.getParameter("cardId"));
        if (cardId < 1) return error("缺少id参数");
        return toAjax(cardCodeService.closeByCardId(cardId));
    }

    /**
     * 作废或恢复
     */
    @PostMapping("/close.json")
    @ResponseBody
    public AjaxResult close(HttpServletRequest request) {
        long id = TKConvert.toLong(request.getParameter("id"));
        String status = TKString.trim(request.getParameter("status"));
        if (id < 1) {
            return error("参数异常");
        }
        if (!status.equals(CardStatus.CLOSE.toString()) && !status.equals(CardStatus.INIT.toString())) {
            return error("参数异常");
        }
        CardCode cardCode = cardCodeService.getById(id);
        if (cardCode == null) {
            return error("充值卡不存在！");
        }
        if (cardCode.getStatus().equals(CardStatus.USED.toString())) {
            return error("充值卡已经被使用！");
        }
        cardCode.setStatus(status);
        int row = cardCodeService.update(cardCode);
        return toAjax(row);
    }
}
